package com.wwj.springboot.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.springboot.entity.CmfzBanner;
import com.wwj.springboot.mapper.CmfzBannerMapper;
import com.wwj.springboot.service.CmfzBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CmfzBannerServiceImpl extends ServiceImpl<CmfzBannerMapper, CmfzBanner> implements CmfzBannerService{

    @Autowired
    private  CmfzBannerMapper cmfzBannerMapper;
    @Override
    @Transactional(readOnly = true)
    public Map selectByPage(int page, int limit) {
        Page<CmfzBanner> cmfzAdminPage = new Page<>(page,limit);
        Page<CmfzBanner> pages = cmfzBannerMapper.selectPage(cmfzAdminPage, null);

        List<CmfzBanner> cmfzBanners = pages.getRecords();
        long total = pages.getTotal();
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",total);
        map.put("data",cmfzBanners);
        return map;

    }

    @Override
    @Transactional(readOnly = true)
    public CmfzBanner selectOne(int id) {
        return cmfzBannerMapper.selectById(id);
    }

    @Override
    public Map deleteBanner(int id) {
        Map map = new HashMap();
        try {
            cmfzBannerMapper.deleteById(id);
            map.put("delete",true);
        } catch (Exception exception) {
            map.put("delete",false);
            exception.printStackTrace();
        }
        return map;
    }

    @Override
    public Map addBanner(CmfzBanner banner) {
        Map map = new HashMap();
        try {
            cmfzBannerMapper.insert(banner);
            map.put("insert",true);
        } catch (Exception exception) {
            map.put("insert",false);
            exception.printStackTrace();
        }
        return map;
    }

    @Override
    public Map updateBanner(CmfzBanner banner) {
        Map map = new HashMap();
        try {
            cmfzBannerMapper.updateById(banner);
            map.put("update",true);
        } catch (Exception exception) {
            map.put("update",false);
            exception.printStackTrace();
        }
        return map;
    }

    @Override
    public List<CmfzBanner> selectAll() {
        return cmfzBannerMapper.selectList(null);
    }


    @Override
    public Map uploadList(List<CmfzBanner> cmfzBanners) {
        Map map = new HashMap();
        try {
            cmfzBannerMapper.uploadList(cmfzBanners);
            map.put("insert",true);
        } catch (Exception exception) {
            map.put("insert",false);
            exception.printStackTrace();
        }
        return map;
    }
}
